package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 服务项目服务impl
 *
 * @author RNIZ
 * @date 2021/11/13
 */
@Service
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.get(id);
    }


    @Override
    public void save(ServiceItem serviceItem) {
        System.out.println(serviceItem.getCarPackage() + "--------------------------------");
        //判断是否是套餐
        if (ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        } else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        serviceItem.setCreateTime(new Date());
        System.out.println(serviceItem.getCarPackage());
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.delete(dictId);
        }
    }

    @Override
    public void saleOne(Long id) {
        //合理化校验
        ServiceItem oldService = serviceItemMapper.get(id);
        if (ServiceItem.SALESTATUS_ON.equals(oldService.getSaleStatus())) {
            throw new BusinessException("商品已经上架");
        }
        if (ServiceItem.CARPACKAGE_YES.equals(oldService.getCarPackage()) &&
                !ServiceItem.AUDITSTATUS_APPROVED.equals(oldService.getAuditStatus())) {
            throw new BusinessException("商品需要审核");
        }
        serviceItemMapper.changeBySaleStatu(id, ServiceItem.SALESTATUS_ON);

    }

    @Override
    public void saleOff(Long id) {
        serviceItemMapper.changeBySaleStatu(id, ServiceItem.SALESTATUS_OFF);

    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        ServiceItem serviceItem = this.get(id);
        if (ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus()) ||
                ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage()) ||
                !(ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getAuditStatus()))) {
            throw new BusinessException("非法操作");
        }
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        //创建时间
        carPackageAudit.setCreateTime(new Date());
        //审核人ID
        carPackageAudit.setAuditorId(director);
        //流程定义ID
        carPackageAudit.setBpmnInfoId(bpmnInfoId);
        //操作人
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        //备注信息
        carPackageAudit.setInfo(info);
        //服务单项ID
        carPackageAudit.setServiceItemId(id);
        carPackageAudit.setServiceName(serviceItem.getName());
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAuditService.save(carPackageAudit);
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("discountPrice",carPackageAudit.getServiceItemPrice().longValue());
        map.put("director",director);
        if (finance!=null) {
            map.put("finance",finance);
        }
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(),
                String.valueOf(carPackageAudit.getId()), map);
        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditService.updateByInstanceId(carPackageAudit);
        serviceItemMapper.updateByAudit(id,ServiceItem.AUDITSTATUS_AUDITING);

    }

    @Override
    public void updateByStatus(Long serviceItemId, Integer auditstatusInit) {
        serviceItemMapper.updateByAudit(serviceItemId,auditstatusInit);
    }

    @Override
    public void update(ServiceItem serviceItem) {
        //未下架商品必须下架
        if (ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())) {
            throw new BusinessException("请先下架商品");
        }
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) &&
                ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus())) {
            throw new BusinessException("商品审核中");

        }
//        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) &&
//                ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())) {
//            throw new BusinessException("审核通过直接上架");
//
//        }
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) &&
                ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItemMapper.update(serviceItem);
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }


}
